from app import app
from flask import render_template,request,redirect
from models import Person
import hashlib
from functools import wraps
from models import *
from datetime import datetime
import os
import time
# 对用户密码进行加密
def set_password(pa):
    md5 = hashlib.md5()
    md5.update(pa.encode())
    return md5.hexdigest()


# 判断是否登陆
def is_login(func):
    @wraps(func)
    def inner(*args, **kwargs):
        name = request.cookies.get('name')
        if name and Person.query.filter(Person.P_name == name):
            return func(*args, **kwargs)
        else:
            return redirect('/')

    return inner

# 性别过滤器
def gender_format(gender):
    return '男' if gender else '女'


# 添加自定义过滤器
app.add_template_filter(gender_format, 'gender_f')


# 首页
@app.route('/')
def index():
    news_list = News.query.filter().order_by(News.id.desc()).limit(5)
    return render_template('index.html',**locals())


# 个人中心
@app.route('/profile/')
def profile():
    return render_template('profile.html')


# 登录
@app.route('/login/')
def login():
    return render_template('login.html')

# 注册
@app.route('/register/')
def register():
    return render_template('register.html')
# 新闻管理页面
@app.route('/news/')
def news():
    news_list = News.query.filter().order_by(News.releaseTime.desc())
    return render_template('news.html', **locals())


# 添加新闻
@app.route('/add_news/', methods=['GET', 'POST'])
def add_news():
    if request.method == 'POST':
        # 保存文字和时间信息
        picture = request.files.get('picture')
        filename = picture.filename
        N = News()
        N.title = request.form.get('title')
        N.author = request.form.get('author')
        N.content = request.form.get('content')
        N.releaseTime = datetime.fromtimestamp(int(time.time()))
        # 处理图片信息
        if filename:
            path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
            file_path = os.path.join('img', filename)
            save_path = os.path.join(path, file_path)
            picture.save(save_path)
            N.newsImg = file_path
        N.save()
        return redirect('/news/')

    return render_template('add_news.html')


# 新闻详细内容
@app.route('/news/<d>/')
def detail_news(d):
    main_news = News.query.filter(News.id == int(d)).all()[0]
    return render_template('detail_news.html', **locals())


# 删除新闻
@app.route('/delete_news/<t>/')
def delete_news(t):
    N = News
    d_news = N.query.filter(News.id == int(t)).all()[0]
    d_news.delete()
    return redirect('/news/')


# 编辑新闻
@app.route('/edit_news/<s>/', methods=['POST', 'GET'])
def edit_news(s):
    N = News()
    M_news = N.query.filter(News.id == int(s)).first()
    if request.method == 'POST':
        # 保存文字和时间信息
        picture = request.files.get('picture')
        filename = picture.filename
        M_news.title = request.form.get('title')
        M_news.author = request.form.get('author')
        M_news.content = request.form.get('content')
        M_news.releaseTime = datetime.fromtimestamp(int(time.time()))
        # 处理图片信息
        if filename != '':
            path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
            file_path = os.path.join('img', filename)
            save_path = os.path.join(path, file_path)
            picture.save(save_path)
            M_news.newsImg = file_path
        M_news.update()
        return redirect('/news/')
    return render_template('edit_news.html', **locals())

# 权限管理
@app.route('/permission/')
def permission():
    return render_template('permission.html')


# 修改权限
@app.route('/edit_permission/')
def edit_permission():
    return render_template('edit_permission.html')


# 关联职位
@app.route('/position_permission/')
def position_permission():
    return render_template('position_permission.html')


# 添加权限
@app.route('/add_permission/')
def add_permission():
    return render_template('add_permission.html')


# 个人考勤
@app.route('/attendance_me/')
def attendance_me():
    return render_template('attendance_me.html')


# 下属考勤
@app.route('/attendance_subordinate/')
def attendance_subordinate():
    return render_template('attendance_subordinate.html')

# 部门
@app.route('/department/')
def department():
    d_list = Department.query.order_by(Department.id.desc())
    return render_template('department.html',**locals())
# 添加部门
@app.route('/add_department/',methods=['POST','GET'])
def add_department():
    if request.method == 'POST':
        dep = request.form
        d_name = dep.get('name')
        d_dscrpt = dep.get('description')
        department = Department()
        department.name = d_name
        department.desc = d_dscrpt
        department.save()
        return redirect('/department/')
    return render_template('add_department.html',**locals())

#删除部门
@app.route('/delete_department/<int:id>/')
def delete_department(id):
    depart = Department.query.get(id)
    Department.delete(depart)
    return redirect('/department/')

# 修改部门
@app.route('/edit_department/<int:id>/',methods=['POST','GET'])
def edit_department(id):
    dep = Department.query.get(id)
    if request.method == 'POST':
        dep = request.form
        d_name = dep.get('name')
        d_dscrpt = dep.get('description')
        department = Department()
        department.name = d_name
        department.desc = d_dscrpt
        department.save()
        de = Department.query.get(int(id))
        Department.delete(de)
        return redirect('/department/')
    return render_template('edit_department.html',**locals())


# 查看职位
@app.route('/position/')
def position():
    return render_template('position.html')


# 职员管理
@app.route('/person/')
def person():
    return render_template('person.html')


# 职员管理-详情
@app.route('/detail_person/')
def detail_person():
    return render_template('detail_person.html')


# 职员管理-编辑
@app.route('/edit_person/')
def edit_person():
    return render_template('edit_person.html')


# 职员管理-添加
@app.route('/add_person/')
def add_person():
    return render_template('add_person.html')
